package com.cloudkinto.service.consumer;

import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.service.consumer.vo.makeshop.MakeShopMarkStatusConsumerReq;
import com.cloudkinto.service.consumer.vo.makeshop.MakeShopMarkShipConsumerReq;
import com.cloudkinto.service.consumer.vo.makeshop.MakeShopOrderUpdateConsumerReq;
import com.cloudkinto.service.consumer.vo.makeshop.MakeshopConsumerReq;
import com.cloudkinto.service.platform.PlatformOrderMakeshopSyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class PlatformOrderMakeShopConsumer {

    private final PlatformOrderMakeshopSyncService platformOrderMakeshopSyncService;

    /**
     * 同步订单表
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_MakeShop_Sync))
    public void syncOrderMakeshop(String msg) {
        MakeshopConsumerReq req = JSONObject.parseObject(msg, MakeshopConsumerReq.class);
        String s = platformOrderMakeshopSyncService.updateMakeShopOrders(req);
        if (StringUtils.isNotBlank(s)) {
            log.error(s);
        }
    }


    /**
     * 更新订单表
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_MakeShop_Update))
    public void syncOrderMakeshopUpdate(String msg) {
        MakeShopOrderUpdateConsumerReq req = JSONObject.parseObject(msg, MakeShopOrderUpdateConsumerReq.class);
        platformOrderMakeshopSyncService.consumerUpdate(req);
    }


    /**
     * 标发
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_MakeShop_MarkShip))
    public void markShip(String msg) {
        MakeShopMarkShipConsumerReq req = JSONObject.parseObject(msg, MakeShopMarkShipConsumerReq.class);
        try {
            platformOrderMakeshopSyncService.markShip(req);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    /**
     * 标记订单状态
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_MakeShop_MarkStatus))
    public void markShipFinish(String msg) {
        MakeShopMarkStatusConsumerReq req = JSONObject.parseObject(msg, MakeShopMarkStatusConsumerReq.class);
        try {
            platformOrderMakeshopSyncService.markOrderStatus(req);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }


}
